﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Control_Searcher
{
    public class ClassParser //parser klassen igennem der holder definitionen for formen for at finde dens navn og dens namespace så subjects.cs kan få en using (til eventuelle namespaces) og formnavnene til at starte en ny instans
    {
        public static List<string> findFormNames(string path)
        {
            List<String> list = new List<string>();
            StreamReader reader = new StreamReader(path);
            string line = string.Empty;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("class") && line.Contains("partial"))
                {
                    int startIdx = line.IndexOf("partial") + 7;
                    list.Add(line.Substring(startIdx).Replace(" ", ""));
                }
                else if (line.Contains("class") && line.Contains("Form"))
                {
                    int startIdx = line.IndexOf("class") + 5;
                    int endIdx = line.IndexOf(":");
                    list.Add(line.Substring(startIdx, endIdx - startIdx).Replace(" ", ""));
                }
            }
            reader.Close();
            return list;
        }

        public static List<string> findFormNamesAndNameSpaces(string path, out List<string> namespaceList)
        {
            List<String> list = new List<string>();
            StreamReader reader = new StreamReader(path);
            string line = string.Empty;
            List<string> nsList = new List<string>();
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("namespace ") && !line.Contains("\"") && !line.Contains("="))
                {
                    int startIdx = line.IndexOf("namespace ") + 10;
                    int endIdx = line.IndexOf(" ", startIdx);
                    if (endIdx < startIdx)
                        nsList.Add(line.Substring(startIdx));
                    else
                        nsList.Add(line.Substring(startIdx, endIdx - startIdx));
                }
                
                 if (line.Contains("class") && line.Contains("Form"))
                {
                    int startIdx = line.IndexOf("class") + 5;
                    int endIdx = line.IndexOf(":");
                    list.Add(line.Substring(startIdx, endIdx - startIdx).Replace(" ", ""));
                }
            }
            reader.Close();
            namespaceList = nsList;
            return list;
        }


    }
}
